<?php

// +----------------------------------------------------------------------
// | 公共模型
// +----------------------------------------------------------------------
// | Copyright (c) 2015-2024 http://www.yicmf.com, All rights reserved.
// +----------------------------------------------------------------------
// | Author: 微尘 <yicmf@qq.com>
// +----------------------------------------------------------------------

namespace app\common\model;

use think\facade\Config;
use think\db\Where;

class Common extends Base
{
    
    public function _list($map, $order = null, $asc = false, $field = '')
    {
        // 排序字段 默认为主键名
        if (isset($_REQUEST['orderField'])) {
            $order = $_REQUEST['orderField'];
        }
        if (!isset($map['status'])) {
            $map['status'] = ['>=', -1];
        }
        if ($order == '') {
            $order = $this->getPk();
        }
        $sort = '';
        if (isset($_REQUEST['orderDirection'])) {
            $sort = $_REQUEST['orderDirection'];
        } else {
            $sort = $asc ? 'asc' : 'desc';
        }
        $list_rows = $this->request->has('limit', 'param') ? $this->request->param('limit') : Config::get('paginate.list_rows');
        $page = $this->request->has('page', 'param') ? $this->request->param('page') :1;

        $result = [];
        $result['code'] = 0;
        $result['data'] =$this->where(new Where($map))->order($order.' '.$sort)->limit($list_rows*($page-1),$list_rows*$page)->select();
        $result['count'] = $this->where(new Where($map))->count();
        return $result;
    }
}
